//数据库连接操作
package core

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	hostname = "127.0.0.1" // 服务器地址
	dbname   = "galen"     // 数据库名
	name     = "root"      // 用户名
	pass     = "root"      // 密码
	port     = "3306"      // 端口
	charset  = "utf8"      // 数据库编码
)

func Connection() *gorm.DB {
	dsn := name + ":" + pass + "@tcp(" + hostname + ":" + port + ")/" + dbname + "?charset=" + charset + "&parseTime=True&loc=Local"

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("Failed to connect database:", err)
		return nil
	}

	return db
}
